$(document)
	.ready(
		function() {
			var date = new Date();
			var seperator1 = "-";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if(month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if(strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
			document.getElementById("thrq").value = currentdate;
			var cookie = $.AMUI.utils.cookie;
			var stockname = GetQueryString("stockname");
			var clientname = GetQueryString("clientname");
			var billnumber = GetQueryString("billnumber");
			var section = GetQueryString("section");
			var maxnumber = GetQueryString("maxnumber");
			var c_id = GetQueryString("c_id");
			var stockno = GetQueryString("stockno");
			var remark = GetQueryString("remark");
			if(section != "") {
				document.getElementById("section").value = section;
			} else {
				document.getElementById("tsection").style.display = "none";
			}
			document.getElementById("stockno").value = stockno;
			document.getElementById("stockname").value = stockname;
			document.getElementById("clientname").value = clientname;
			document.getElementById("billnumber").value = billnumber;
			document.getElementById("maxnumber").value = maxnumber;
			document.getElementById("c_id").value = c_id;

			var span = "<span style='color:sandybrown'><剩余吨数：" +
				maxnumber + "吨></span>";
			$("#maxnumber1").append(span);
			// 获取当前用户的司机
			$.getJSON("json/locat.json",
				function(data) {
					var locat = data["url"];
					var factoryid = data["factoryid"];
					var companyid = data["companyid"];
					var data ,x = "";
					$
						.ajax(
							locat +
							"/appClient/driver_list?companyid=" +
							companyid, {
								type: "POST",
								xhrFields: {
									withCredentials: true,
									useDefaultXhrHeader: false
								},
								crossDomain: true,
								success: function(data, status, xhr) {
									if("error" == data["msg"]) {
										window.location.href = "login.html";
									} else if(stockno == "" || stockno == null) {
										window.location.href = "cwddlb.html";
									} else {
										//司机
										if("drierror" == data["msg"]) {
											swal("没有司机不能提货 ", "先去添加司机", "error");
											swal({
												title: "您还没有提货司机",
												text: "是否添加提货司机",
												type: "warning",
												showCancelButton: true,
												closeOnConfirm: false,
												cancelButtonText: "取消",
												confirmButtonText: "确定",
												confirmButtonColor: "#ec6c62"
											}, function() {
												window.location.href = "tjsja.html";
											})

										} else {
												var arrs=data.list_driver;
												$.each(arrs,function(i,item){		
												x+="<option value='"+item.d_id+"'>"+item.tracknumber +"||"+item.name +"||"+item.phone+"</option>";				
												}); 
											    $("#driverid").append(x) ;				
											
											
										}
									}

								},
								error: function(
									data) {
									swal(
										"",
										"服务异常！",
										"error");
									window.location.href = locat +
										"/clientUser/tologin";
								}
							});
				});

		});

function driverNo(s) {
	var tracknumber = s;
	var cookie = $.AMUI.utils.cookie;
	$.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		var factoryid = data["factoryid"];
		var companyid = data["companyid"];
		$.ajax(locat + "/clientOrder/driverNo", {
			type: "POST",
			xhrFields: {
				withCredentials: true,
				useDefaultXhrHeader: false
			},
			data: {
				tracknumber: tracknumber
			},
			crossDomain: true,
			success: function(data, status, xhr) {
				jQuery("#drivername").empty();
				document.getElementById("drivername").options.add(new Option("请选择", ""));
				for(i = data["listdriver"].length - 1; i >= 0; i--) {
					document.getElementById("drivername").options.add(new Option(
						data["listdriver"][i]["name"] + " " + data["listdriver"][i]["phone"],
						data["listdriver"][i]["idnumber"]));
				}

			},
			error: function(data) {
				swal("失败");
				sweetAlert("服务异常", "请稍后再试", "error");
				window.location.href = "login.html";

			}
		});
	});

}

/*jQuery(function() {
	var cookie = $.AMUI.utils.cookie;
	var sessionId = (GetQueryString("sessionId") == null) ? "" : GetQueryString("sessionId");
	var factoryId = (GetQueryString("factoryid") == null) ? "" : GetQueryString("factoryid");
	var stockname = unescape(GetQueryString("stockname"));
	var clientname = unescape(GetQueryString("clientname"));
	var billnumber = GetQueryString("billnumber");
	var maxnumber = GetQueryString("maxnumber");
	var c_id = GetQueryString("c_id");
	var stockno = GetQueryString("stockno");
	document.getElementById("stockno").value = stockno;
	document.getElementById("stockname").value = stockname;
	document.getElementById("clientname").value = clientname;
	document.getElementById("billnumber").value = billnumber;
	document.getElementById("maxnumber").value = maxnumber;
	document.getElementById("c_id").value = c_id;
	var span = "<span style='color:sandybrown'><剩余吨数：" + maxnumber + "吨></span>";
	$("#maxnumber1").append(span);
	//获取当前用户的司机
	$.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		var factoryid = data["factoryid"];
		if(null == sessionId || "" == sessionId || "" == sessionId) {
			window.location.href = "login.html";
		} else {
			$.ajax(locat + "/clientUser/driver_list?sessionId=" + sessionId, {
				type: "GET",
				xhrFields: {
					withCredentials: true,
					useDefaultXhrHeader: false
				},
				crossDomain: true,
				success: function(data, status, xhr) {
					if("error" == data["msg"]) {
						window.location.href = "login.html";
					} else if(stockno == "" || stockno == null) {
						window.location.href = "cwddlb.html";
					} else {
						//司机
						if("drierror" == data["msg"]) {
							swal("没有司机不能提货 ", "先去添加司机", "error");
							swal({
								title: "您还没有提货司机",
								text: "是否添加提货司机",
								type: "warning",
								showCancelButton: true,
								closeOnConfirm: false,
								cancelButtonText: "取消",
								confirmButtonText: "确定",
								confirmButtonColor: "#ec6c62"
							}, function() {
								window.location.href = "tjsja.html";
							})

						} else {
							for(i = data["list_driver"].length - 1; i >= 0; i--) {
								document.getElementById("driverid").options.add(new Option(data["list_driver"][i]["name"], data["list_driver"][i]["d_id"]));
							}
						}
					}

				},
				error: function(data) {
					swal("", "服务异常！", "error");
					window.location.href = login.html;
				}
			});
		}

	});

});*/






function save() {

	var billnumber = $("#billnumber").val();
	var c_id = $("#c_id").val();
	var stockno = $("#stockno").val();
	var stockname = $("#stockname").val();
	var driverid = $("#driverid").val();
	var clientname = $("#clientname").val();
	var goodsnumber = $("#goodsnumber").val();
	var thrq = $("#thrq").val();
	var thrqmsg = $("#thrqmsg").val();
	var numbermsg = $("#numbermsg").val();
	var toaddress = $("#toaddress").val();
	var remark = $("#remark").val();
	var section = $("#section").val();
	var idnumber=$("#drivername").val();
	var radionum = document.getElementsByName("pdradio");
	for(var i=0;i<radionum.length;i++){
		 if(radionum[i].checked){
		 var msg = radionum[i].value;
		 if("yes"==msg){
			 var hdbillnumber=$("#hdbillnumber").val();
			 if(hdbillnumber==null||hdbillnumber==""||hdbillnumber=="null"){
				 swal("请选择合单票号", "", "error")
					return false;
				}
			 }else{
				 var hdbillnumber="-1";
			 }
		 }
		 }
	var cookie = $.AMUI.utils.cookie;
	var clientNumber = cookie.get("clientNumber");
	if (billnumber == "" || c_id == "" || stockname == "" || driverid == "-1"
			|| goodsnumber == "" || thrq == "" || toaddress =="") {
				swal("填写有误");
		return false;
	} else {
		if (thrqmsg == "error" || numbermsg == "error") {
			swal("填写有误，请认真填写", "", "error")
		} else {
			var id = "body-loading";
			$(window).loading({
				action : "open",
				id : id,
				font : 40
			});
			var cookie = $.AMUI.utils.cookie;
			$.getJSON("json/locat.json", function(
					data) {
					var locat = data["url"];
				var factoryid = GetQueryString("factoryId");
				var companyid = data["companyid"];
				$.ajax(locat + "/clientOrder/place_order", {
					type : "POST",
					xhrFields : {
						withCredentials : true,
						useDefaultXhrHeader : false
					},
					data : {
						billnumber : billnumber,
						c_id : c_id,
						stockno : stockno,
						stockname : stockname,
						driverid : driverid,
						clientname : clientname,
						goodsnumber : goodsnumber,
						thrq : thrq,
						factoryid : factoryid,
						remark : remark,
						toaddress : toaddress,
						idnumber : idnumber,
						companyid : companyid,
						hdbillnumber : hdbillnumber,
						section : section
					},
					crossDomain : true,
					success : function(data, status, xhr) {
						var time = setTimeout(function() {
							// 关闭
							$(window).loading({
								action : "close",
								id : id
							});
						}, 100);
						if ("success" == data["msg"]) {
							cookie.unset("stockno");
							cookie.unset("stockname");
							cookie.unset("billnumber");
							cookie.unset("maxnumber");
							cookie.unset("clientname");
							cookie.unset("clientnumber");
							cookie.unset("c_id");
							swal({
								title : "下单成功",
								text : "要继续下这个品种的订单吗？",
								type : "warning",
								showCancelButton : true,
								confirmButtonColor : "#DD6B55",
								confirmButtonText : "继续下单",
								cancelButtonText : "查看订单",
								closeOnConfirm : false,
								closeOnCancel : false
							}, function(isConfirm) {
								if (isConfirm) {
									window.location.href = "xthd.html?factoryId=" + factoryId+"&stockno="+stockno+"&section="+section+"&stockname="+stockname+"&billnumber="+billnumber+"&maxnumber="+maxnumber+"&clientname="+clientname+"&clientnumber="+clientnumber+"&c_id="+c_id;
								} else {
									window.location.href = "lsdd.html";
								}
							});

						} else {
							sweetAlert("下单异常", "请稍后再试", "error");
						}

					},
					error : function(data) {
						var time = setTimeout(function() {
							// 关闭
							$(window).loading({
								action : "close",
								id : id
							});
						}, 100);
						sweetAlert("服务异常", "请稍后再试", "error");
						window.location.href ="user.html";

					}
				});
			});

		}
	}
	// 提货单保存

}




function chkvalue(txt) {
	var cookie = $.AMUI.utils.cookie;
	var maxgoodnumber = Number(GetQueryString("maxnumber"));
	//swal(maxgoodnumber+"sasasasa"+txt.value+"ssssss");
	var goodsnumber = Number(txt.value);

	if(maxgoodnumber < goodsnumber) {
		swal("提货吨数不能大于剩余货物吨数", "", "error");
		$("#goodsnumber").css({
			"color": "red",
		});
		document.getElementById("numbermsg").value = "error";
	} else {
		if(txt.value <= 0) {
			swal("提货吨数不能小于或等于0", "", "error");
			$("#goodsnumber").css({
				"color": "red",
			});
			document.getElementById("numbermsg").value = "error";
		} else {
			$("#goodsnumber").css({
				"color": "black",
			});
			document.getElementById("numbermsg").value = "success";
		}

	}
}

function chkDate() {
	var pdate = document.getElementById('thrq');
	var d = new Date;
	var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());

	var reg = /\d+/g;
	var temp = pdate.value.match(reg);

	var foday = new Date(temp[0], parseInt(temp[1]) - 1, temp[2]);

	if(foday < today) {
		swal("提货日期不能在今天之前", " ", "error");
		$("#thrq").css({
			"color": "red",
		});
		document.getElementById("thrqmsg").value = "error";

	} else {
		$("#thrq").css({
			"color": "black",
		});
		document.getElementById("thrqmsg").value = "success";
	}

}

function goback() {
	var factoryId = GetQueryString("factoryId");
	window.location.href = 'cwddlb.html?factoryId=' + factoryId;

}

function GetQueryString(name) {
	  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");   
      var r = window.location.search.substr(1).match(reg);   
      if (r != null) return decodeURI(r[2]); return null;  
}